<template>
  <div class="main" style="margin: 10px">
    <el-card>
      <div>
        <!--工具栏-->
        <div class="head-container">
          <div>
            <span>
              <el-select v-model="dataSourceKey" placeholder="请选择数据源" style="width: 200px;margin:0 15px" class="filter-item">
                <el-option v-for="item in dataSourceOptions" :key="item.id" :label="item.name" :value="item.name">
                </el-option>
              </el-select>
              <span class="filter-item">
                <el-button type="success" icon="el-icon-search" @click="searchFilter">搜索</el-button>
                <!-- <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button> -->
              </span>
            </span>
          </div>
        </div>
      </div>
      <el-card style="margin-top: 10px">
        <el-table border ref="table" :data="tableData" style="width: 100%" row-key="id" lazy>>
          <el-table-column type="index" label="#" width="55"> </el-table-column>
          <el-table-column prop="tableName" label="表名称"> </el-table-column>
          <el-table-column prop="tableComment" label="表注释" align="center"></el-table-column>
          <el-table-column prop="createTime" label="创建日期" align="center">
            <template slot-scope="scope">
              {{ scope.row.createTime | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="250" align="center">
            <template slot-scope="scope">
              <el-button type="primary" @click="handleSee(scope.row)">字段</el-button>
              <el-button type="primary" @click="handleGenerate(scope.row)">生成</el-button>
              <el-button type="primary" @click="handleEdit(scope.row)">设计</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </el-card>
    <!-- 分页 -->
    <pagination :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList"/>
    <FieldPreviewDialog :fieldPreviewDialog='fieldPreviewDialog' :formData='formData'></FieldPreviewDialog>
    <GenerateDialog :generateDialog='generateDialog' :formData='formData' @previewHandleView='previewHandleView' @download='download'></GenerateDialog>
    <el-dialog :title="preview.title" :visible.sync="preview.open" width="80%" top="5vh" append-to-body>
      <Preview :queryData="this.formData" v-if="preview.open"/>
    </el-dialog>
  </div>
</template>

<script>
import { getList, getTablePageList, handleDown } from "@/api/system/dataSource";
import Pagination from "@/components/Pagination"; // el分页
import FieldPreviewDialog from "@/components/System/gen/fieldPreviewDialog";
import GenerateDialog from "@/components/System/gen/generateDialog";
import Preview from './preview'

export default {
  data() {
    return {
      AddBtnAuth: ["add"],
      tableData: [],
      loading: false,
      fieldPreviewDialog: {
        show: false,
        title: ""
      },
      generateDialog: {
        show: false,
        title: ""
      },
      //条件查询搜索
      total: 0,
      listQuery: {
        current: 1,
        size: 10,
        dsName: undefined,
      },
      formData: {},
      dataSourceKey: "",
      dataSourceOptions: [],
      // 预览参数
      preview: {
        open: false,
        title: "代码预览"
      },
    };
  },
  methods:{
    getList() {
      getTablePageList(this.listQuery).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
        this.listQuery.current = res.data.current;
        this.listQuery.size = res.data.size;

        setTimeout(() => {
          this.listLoading = false;
        }, 1.5 * 1000);
      });
    },
    //获取所有数据源
    getDataSourceList() {
      getList().then((res) => {
        this.dataSourceOptions = res.data;
        setTimeout(() => {
          this.listLoading = false;
        }, 1.5 * 1000);
      });
    },
    // 字段预览按钮触发
    handleSee(row) {
      this.fieldPreviewDialog = {
        show: true,
        title: "字段预览"
      },
      this.formData = {
        tableName: row.tableName,
        dsName: this.dataSourceKey,
      };
    },
    //生成
    handleGenerate(row) {
      this.generateDialog = {
        show: true,
        title: "生成配置"
      },
      this.formData = {
        tableName: row.tableName,
        dsName: this.dataSourceKey,
        style: "1"
      };
    },
    //预览
    previewHandleView() {
        this.preview.open = true
    },
    //下载
    download() {
      handleDown(this.formData).then(() => {
        console.log()
        // this.box = false
      })
    },
    searchFilter() {
      if (this.dataSourceKey != "") {
        this.listQuery.dsName = this.dataSourceKey;
      }
      this.getList();
    },
  },
  mounted() {
    this.getDataSourceList();
    this.getList();
  },
  components: {
    Pagination,
    FieldPreviewDialog,
    GenerateDialog,
    Preview,
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-switch.is-disabled {
  opacity: 1;
}
::v-deep .el-switch.is-checked .el-switch__core {
  &:hover {
    cursor: pointer;
  }
}
// 工具栏
// .head {
//   &-container {
//     text-indent: -10px;
//     > div {
//       display: flex;
//       align-items: center;
//       justify-content: space-between;
//       > span {
//         display: flex;
//         align-items: center;
//       }
//       .filter {
//         &-item {
//           margin-left: 10px;
//         }
//       }
//     }
//   }
// }
</style>